class Solution 
{
public:
    vector<int> searchRange(vector<int>& nums, int target) 
    {
        int n = nums.size();
        // 特殊情况处理
        if(!n) return {-1, -1};
        // 1、二分找左端点
        int left = 0, right = n - 1, begin = -1;
        while(left < right)
        {
            int mid = left + (right - left) / 2;
            if(nums[mid] < target) left = mid + 1;
            else right = mid;
        }
        // 2、判断是否有结果 && 标记左端点
        if(nums[left] != target) return {-1, -1};
        else begin = left; 
        // 3、二分找右端点
        right = n - 1;
        while(left < right)
        {
            int mid = left + (right - left + 1) / 2;
            if(nums[mid] <= target) left = mid;
            else right = mid - 1;
        }
        // 4、返回最终结果
        return {begin, right};
    }
};